//
//  ViewController.m
//  UI04_TomCat
//
//  Created by 洛洛大人 on 16/6/9.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()
/** 声明属性 */
@property(nonatomic, retain)UIImageView *imageViewOfBack;


@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self creatAnimationImage];
    [self scratchButton];
    [self pieButton];
    [self fartButton];
    [self eatButton];
    [self drinkButton];
    [self cymbalButton];
    [self knockoutButton];
    [self angryButton];
    [self angryOneButton];
    [self footleftButton];
    [self footrightButton];
    [self stomachButton];
  
  
}


/** 设置背景图片 */
-(void)creatAnimationImage {
    
    self.imageViewOfBack = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.imageViewOfBack.image = [UIImage imageNamed:@"scratch_00.jpg"];
    [self.view addSubview:self.imageViewOfBack];
    [self.imageViewOfBack release];
}

//  scratchbutton
-(void)scratchButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   
    NSString *path = [[NSBundle mainBundle]pathForResource:@"scratch"ofType:@"png"];
    
    [button setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];
    
    button.frame = CGRectMake(10, 400, 50, 50);
    
    [self.view addSubview:button];
    [button addTarget:self action:@selector(scratchAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)scratchAction:(UIButton *)button {
    [self actionNumber:55 withName:@"scratch"];
}




//  piebutton
-(void)pieButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   
    NSString *path = [[NSBundle mainBundle]pathForResource:@"pie"ofType:@"png"];
   
    [button setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];
   
    button.frame = CGRectMake(10, 500, 50, 50);
    
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(pieAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)pieAction:(UIButton *)button {
    [self actionNumber:23 withName:@"pie"];
}




//  fartbutton
-(void)fartButton {
   
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"fart"ofType:@"png"];
    
    [button setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];
    
    button.frame = CGRectMake(10, 600, 50, 50);
    
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(fartAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)fartAction:(UIButton *)button {
    [self actionNumber:27 withName:@"fart"];
}




//  eatbutton
-(void)eatButton {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"eat"ofType:@"png"];
    
    [button setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];
    
    button.frame = CGRectMake(350, 400, 50, 50);
    
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(eatAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [button release];
}
-(void)eatAction:(UIButton *)button {
    [self actionNumber:39 withName:@"eat"];
}



//  drinkbutton
-(void)drinkButton {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"drink"ofType:@"png"];
    
    [button setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];
    
    button.frame = CGRectMake(350, 500, 50, 50);
    
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(drinkAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)drinkAction:(UIButton *)button {
    [self actionNumber:80 withName:@"drink"];
}




//  cymbalbutton
-(void)cymbalButton {
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    NSString *path = [[NSBundle mainBundle]pathForResource:@"cymbal"ofType:@"png"];
   
    
    [button setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];
    
    button.frame = CGRectMake(350, 600, 50, 50);
    
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(cymbalAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)cymbalAction:(UIButton *)button {
    [self actionNumber:12 withName:@"cymbal"];
}




//  koockoutbutton
-(void)knockoutButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   
    button.frame = CGRectMake(130, 130, 150, 200);
    [self.view addSubview:button];

        [button addTarget:self action:@selector(koockoutAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)koockoutAction:(UIButton *)button {
    [self actionNumber:80 withName:@"knockout"];
}



//  angrybutton
-(void)angryOneButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
   
    button.frame = CGRectMake(130, 600, 150, 60);
    
    [self.view addSubview:button];
    
    [button addTarget:self action:@selector(angryOneAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)angryOneAction:(UIButton *)button {
    [self actionNumber:25 withName:@"angry"];
}

-(void)angryButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(130, 390, 150, 60);

    [self.view addSubview:button];

    
    [button addTarget:self action:@selector(angryAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)angryAction:(UIButton *)button {
    [self actionNumber:25 withName:@"angry"];
}


//  footleftbutton
-(void)footleftButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(150, 650, 50, 60);
    [self.view addSubview:button];

    [button addTarget:self action:@selector(footleftAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)footleftAction:(UIButton *)button {
    [self actionNumber:29 withName:@"footright"];
}



//  footrightbutton
-(void)footrightButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(210, 650, 50, 60);
    
    [self.view addSubview:button];

    [button addTarget:self action:@selector(footrightAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)footrightAction:(UIButton *)button {
    [self actionNumber:29 withName:@"footleft"];
}


//  stomachbutton
-(void)stomachButton {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button.frame = CGRectMake(130, 450, 150, 150);
    
    [self.view addSubview:button];
    
    
    [button addTarget:self action:@selector(stomachAction:) forControlEvents:UIControlEventTouchUpInside];
    [button release];
}

-(void)stomachAction:(UIButton *)button {
    [self actionNumber:33 withName:@"stomach"];
}









 //循环方法
-(void)actionNumber:(NSInteger)count withName:(NSString *)name {
 
    NSMutableArray *mutableArray = [NSMutableArray arrayWithCapacity:count];
    for (int i = 0; i <= count; i++) {
        NSString *scratch = [NSString stringWithFormat:@"%@_%02d.jpg",name, i];
        UIImage *animationImage = [UIImage imageNamed:scratch];
        [mutableArray addObject:animationImage];
    }
    self.imageViewOfBack.animationImages = mutableArray;
    self.imageViewOfBack.animationRepeatCount = 1;
    
    [self.imageViewOfBack startAnimating];

}











- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
